package com.dragon.web.ykd.controller;

import java.util.List;
import java.util.Map;

import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.financial.loan.dao.McfeRepaymentFlowDao;
import com.marvels.financial.loan.dto.EarlyPaymentCalculatDto;
import com.marvels.financial.loan.dto.ReplacePaymentConditionDto;
import com.marvels.financial.loan.service.ReplacePaymentApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.dragon.app.commons.dto.WithholdDTO;
import com.dragon.app.commons.vo.JsonResult;
import com.dragon.app.ykd.dto.AcLoanInfoDTO;
import com.dragon.app.ykd.dto.RepaymentNoMobileDTO;
import com.dragon.app.ykd.service.ClientRepaymentService;
import com.marvels.common.utils.MarvelsLogUtil;


@Controller
@RequestMapping("${platform.name.code}/clientRepayment/")
public class ClientRepaymentController{
	@Autowired
	private ClientRepaymentService clientRepaymentService;
	@Autowired
	private ReplacePaymentApi replacePaymentApi;

	/**
	 * 获取还款信息（新版本）
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getRepaymentInfoNew")
	public JsonResult<List<Map<String, Object>>> getRepaymentInfoNew(@RequestBody AcLoanInfoDTO acLoanInfo, @RequestHeader String accessToken,
			@RequestHeader String idFintechUmUser) {
		MarvelsLogUtil.getInstance().info("getRepaymentInfoNew >>>>>>>>>>>>>>>>>>>>"+JSON.toJSONString(acLoanInfo));
		return clientRepaymentService.getRepaymentInfoNew(acLoanInfo.getLoanId(),accessToken, idFintechUmUser);
	}

	@ResponseBody
	@RequestMapping("repaymentNoMobile")
	public JsonResult<String> repaymentNoMobile(@RequestBody WithholdDTO withholdDTO, @RequestHeader String accessToken, @RequestHeader String idFintechUmUser){
		MarvelsLogUtil.getInstance().info("submit repaymentNoMobile param [withholdDTO = " +JSON.toJSONString(withholdDTO)+"] [idFintechUmUser = "+idFintechUmUser+ "] [accessToken = "+accessToken+"]");
		return clientRepaymentService.repaymentNoMobile(withholdDTO, accessToken, idFintechUmUser);

	}
	@ResponseBody
	@RequestMapping("clientcalculatearlypayment")
	public MarvelsResultDTO calculatEarlypayment(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto, @RequestHeader String accessToken,
												 @RequestHeader String idFintechUmUser) {
		try {
			EarlyPaymentCalculatDto earlyPaymentCalculatDto=replacePaymentApi.calculatEarlypayment(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					earlyPaymentCalculatDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>提前结清试算处理FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>提前结清试算处理异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "提前结清试算处理异常", null);
		}
	}


	@ResponseBody
	@RequestMapping("directPaymentSms")
	public JsonResult<String> directPaymentSms(@RequestBody RepaymentNoMobileDTO repaymentNoMobileDTO, @RequestHeader String accessToken, @RequestHeader String idFintechUmUser){
		MarvelsLogUtil.getInstance().info("submit repaymentNoMobile param [withholdDTO = " +JSON.toJSONString(repaymentNoMobileDTO)+"] [idFintechUmUser = "+idFintechUmUser+ "] [accessToken = "+accessToken+"]");
		return clientRepaymentService.directPaymentSms(repaymentNoMobileDTO, accessToken, idFintechUmUser);

	}

	//还款记录
	@RequestMapping("repay/record")
	@ResponseBody
	public JsonResult<List<Map<String,Object>>> repaymentRecord(Integer pageIndex,Integer pageSize, @RequestHeader String accessToken, @RequestHeader String idFintechUmUser){
		return clientRepaymentService.repaymentRecord(pageIndex, pageSize, accessToken, idFintechUmUser);

	}

}
